package com.itheima.config;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MybatisConfig {


    //spring整合Mybatis
        //动态创建SqlSession对象, 放入spring容器中
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.itheima.bean"); //配置别名
        return sqlSessionFactoryBean;
    }

    @Bean  //扫描mapper包; 为包内的接口动态创建实现类bean对象放入spring容器中
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.itheima.mapper");//扫描指定包
        return mapperScannerConfigurer;//返回动态创建的bean对象放入spring容器中
    }
}
